//
//  MyCustomTabbar.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/7/23.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit


@objc protocol MyCustomTabbarDelegate {
    
    @objc optional func centerButtonClick(tabbar:MyCustomTabbar)
    
}

class MyCustomTabbar: UITabBar{
    

    var centerBtn:UIButton!
    var centerBtnTitle:UILabel!
    
    var dele: MyCustomTabbarDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.shadowImage = UIImage()
        self.backgroundImage = UIImage()
        self.isTranslucent = false
        self.centerBtn = UIButton.init(type: .custom)
        self.centerBtn.setImage(UIImage.init(named: "daohang_cyqd"), for: .normal)
        self.centerBtn.addTarget(self, action: #selector(centerMethod), for: .touchUpInside)
        self.addSubview(self.centerBtn)
        
    }
    
    @objc func centerMethod(){
        self.dele?.centerButtonClick!(tabbar: self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.setShadow(color: "000000", offset: CGSize.init(width: 0, height: -3), radius: 3, opacity: 0.02)
        self.centerBtn.frame = CGRect.init(x: 0, y: 0, width: 94/2, height: 94/2)
        self.centerBtn.centerX = self.centerX
        self.centerBtn.centerY = self.height * 0.5 - 15 - (Utils.isIphoneX() ? 34/2 : 0)
        self.centerBtn.setBorder(color: "ffffff", width: 6)
        self.centerBtn.setCornerRadio(radio: self.centerBtn.width/2, isMask: false)
        
        self.centerBtnTitle = UILabel()
        self.centerBtnTitle.text = "常用清单"
        self.centerBtnTitle.font = UIFont.systemFont(ofSize: 10)
        self.centerBtnTitle.textColor = UIColor.init(hexColor: "#f7c201")
        self.centerBtnTitle.sizeToFit()
        self.centerBtnTitle.centerX = self.centerBtn.centerX
        self.centerBtnTitle.top = self.centerBtn.bottom + 3
        self.addSubview(self.centerBtnTitle)
        
        var btnIndex:CGFloat = 0
        
        let classObj:AnyObject.Type = NSClassFromString("UITabBarButton")!
        
        for btn in self.subviews {
            
            if btn.isKind(of: UIImageView.classForCoder()) && btn.height <= 1 {
                btn.isHidden = true
            }
            
            if btn.isKind(of: classObj){
                btn.width = self.width / 5
                btn.left = btn.width * btnIndex
                btnIndex = btnIndex + 1
                
                if btnIndex == 2 {
                    btnIndex = btnIndex + 1
                }
            }
        }
        self.bringSubview(toFront: self.centerBtn)
        
    }
    
    
}

















